options mprint mlogic symbolgen ls=72 ps=54;

libname b_1999 "/Young_Surgeon/sas/Medicare/1999";
libname b_2000 "/Young_Surgeon/sas/Medicare/2000";
libname b_2001 "/Young_Surgeon/sas/Medicare/2001";
libname b_2002 "/Young_Surgeon/sas/Medicare/2002";
libname b_2003 "/Young_Surgeon/sas/Medicare/2003";
libname b_2009 "/Young_Surgeon/sas/Medicare/2009";
libname b_2010 "/Young_Surgeon/sas/Medicare/2010";
libname b_2011 "/Young_Surgeon/sas/Medicare/2011";
libname b_2012 "/Young_Surgeon/sas/Medicare/2012";
libname b_2013 "/Young_Surgeon/sas/Medicare/2013";
libname f "/Young_Surgeon/sas/sharpe/flags_linkage";
libname z "/Young_Surgeon/sas/zhou/data";
libname b "/Young_Surgeon/sas/sharpe/partb_linkage";
libname c "/Young_Surgeon/sas";
libname h "/Young_Surgeon/sas/hill/comorb";
libname hc "/Young_Surgeon/sas/hill/check";



**************************************************************;
**** Create list of orthopedic and general surgery codes. ****;

%let o_list = ("0070" "0071" "0072" "0073" "8140" "8151" "8152" "8153" "7935"
		"0080" "0081" "0082" "0083" "0084" "8154" "8155");
	
%let g_list = ("4495" "4701" "4709" "4590" "4591" "4592" "4593"
		"4594" "4595" "4673" "4674" "4675" "4679" "8541" "8542" "8543"
		"8544" "8545" "8546" "8547" "8548" "427" "4240" "4241" "4242"
		"5321" "5329" "5122" "5123" "5124" "5132" "5136" "5137" "5141"
		"5151" "443" "4431" "4438" "4439" "5361" "5362" "5363" "5369" "1711"
                "1712" "1713" "1721" "1722" "1723" "1724" "5300" "5301" "5302" "5303"
                "5304" "5305" "5310" "5311" "5312" "5313" "5314" "5315" "5316" "5317"
                "1731" "1732" "1733" "1734" "1735" "1736" "1739" "4571" "4572" "4573"
                "4574" "4575" "4576" "4579" "458" "4581" "4582" "4583" "503" "5022"
                "5029" "5059" "5451" "5459" "4382" "4389" "4468" "4551" "459" "526"
                "527" "5251" "5252" "5253" "5259" "5493" "4849" "485"
                "4850" "4851" "4852" "4859" "4862" "4863" "4869" "4875" "4561" "4562"
                "4563" "415" "4342" "435" "436" "437" "4399" "4429" "4466" "4467" "4469"
                "4441" "4442" "5341" "5342" "5343"
                "5349" "5351" "5359" "0722" "0729" "073" "0681" "0689" "062" "0631" "0639"
                "064" "0650" "0651" "0652");
                
************************************************************;
**** Create a list of non-US state codes for exclusion. ****;

%LET nonus = ("40" "48" "54" "56" "57" "58" "59" "60" "61" "62"
		"63" "64" "65" "66" "97" "98" "99" "94" "96" "95");
                
                
************************************************;
**** Bring in all MBSF and inpatient files. ****;

%let years=(1999 2000 2001 2002 2003 2009 2010 2011 2012 2013);
%let nyears = %sysfunc(countw(&years.));

%macro read_in_mbsf;

	%do i=1 %to &nyears;
		
		**** Set the ith year. ****;
		%let cyear = %scan(&years.,&i.);
		
		**** Read in the master beneficiary summary files. ****;
		data mbsf_ab_summary_&cyear.;
			set b_&cyear..mbsf_ab_summary_&cyear.;
			if (BENE_MDCR_STATUS_CD not in ("20" "21" "31"));
		run;
		
		**** Read in and modify the inpatient files. ****;
		data inpatient_base_claims_j_&cyear.;
			set b_&cyear..inpatient_base_claims_j_&cyear.;
			admit_year=&cyear.;
		
			ortho_adm_trad=0;
			gensurg_adm_trad=0;
			ortho_adm_mod=0;
			gensurg_adm_mod=0;
		
			%if (&cyear.>=1999 and &cyear.<=2003) %then %do;
				era_trad=1;
				era_mod=0;
			
				if ICD_PRCDR_CD1 in &o_list. then ortho_adm_trad=1;
				if ICD_PRCDR_CD1 in &g_list. then gensurg_adm_trad=1;
			
				date1 = "01JUL1999"d;
				date2 = "30JUN2003"d;
				
				if date1 <= CLM_ADMSN_DT <= date2;
			%end;
			%else %if (&cyear. >= 2009) %then %do;
				era_trad=0;
				era_mod=1;
			
				if ICD_PRCDR_CD1 in &o_list. then ortho_adm_mod=1;
				if ICD_PRCDR_CD1 in &g_list. then gensurg_adm_mod=1;
			
				date1 = "01JUL2009"d;
				date2 = "30JUN2013"d;
				
				if date1 <= CLM_ADMSN_DT <= date2;
			%end;
		
		

		
			third_digit_prvdr = substr(PRVDR_NUM, 3, 1);
			sixth_digit_prvdr = substr(PRVDR_NUM, 6, 1);
			
		
			age_at_admsn = (CLM_ADMSN_DT - DOB_DT)/365.25;
		
			**** Create age groups. ****;
			age_65_69=0;
			age_70_74=0;
			age_75_79=0;
			age_80_85=0;
			age_85_plus=0;
			if 65 <= age_at_admsn < 70 then age_65_69=1;
			if 70 <= age_at_admsn < 75 then age_70_74=1;
			if 75 <= age_at_admsn < 80 then age_75_79=1;
			if 80 <= age_at_admsn < 85 then age_80_85=1;
			if age_at_admsn >= 85 then age_85_plus=1;
		
		
			if (CLM_ADMSN_DT^=. and NCH_BENE_DSCHRG_DT^=. and DOB_DT^=. and PRCDR_DT1^=.);
			admit_year=year(CLM_ADMSN_DT);
			disch_year=year(NCH_BENE_DSCHRG_DT);
			birth_dt = DOB_DT;
 			ppx_dt = PRCDR_DT1;
 			data_year=&cyear.;
 			
		
			los = NCH_BENE_DSCHRG_DT - CLM_ADMSN_DT;
			if los=0 then los=1;
		
			emergent_type = 0;
			urgent_type = 0;
			elective_type = 0;
			other_adm_type = 0;
			adm_type_missing = 0;
			if CLM_IP_ADMSN_TYPE_CD='1' then emergent_type=1;
			if CLM_IP_ADMSN_TYPE_CD='2' then urgent_type=1;
			if CLM_IP_ADMSN_TYPE_CD='3' then elective_type=1;
			if CLM_IP_ADMSN_TYPE_CD in ('4','5','6','7','8') then other_adm_type=1;
			if CLM_IP_ADMSN_TYPE_CD in ('0','9') then source_miss=1;
		
			**** Create ppx_: variables. ****;
			%let ppx_list = (4495 4701 4709 4590 4591 4592 4593 4594
			4595 4673 4674 4675 4679 8541 8542 8543 8544 8545 8546 8547
			8548 427 4240 4241 4242 5321 5329 5122 5123 5124 5132 5136 5137 5141
			5151 443 4431 4438 4439 5361 5362 5363 5369 1711 1712 1713
			1721 1722 1723 1724 5300 5301 5302 5303 5304 5305 5310 5311
			5312 5313 5314 5315 5316 5317 1731 1732 1733 1734 1735 1736
			1739 4571 4572 4573 4574 4575 4576 4579 458 4581 4582 4583
			503 5022 5029 5059 5451 5459 4382 4389 4468 4551 459 526 527
			5251 5252 5253 5259 5493 4849 485 4850 4851 4852 4859 4862 4863
			4869 4875 4561 4562 4563 415 4342 435 436 437 4399 4429 4466
			4467 4469 4441 4442 5341 5342 5343 5349 5351 5359 0722 0729
			073 0681 0689 062 0631 0639 064 0650 0651 0652 0070 0071 0072 0073
			8140 8151 8152 8153 7935 0080 0081 0082 0083 0084 8154 8155);
		 
		 	%macro ppx_loop;
		 
		 		%local i next_ppx;
		 		%do i=1 %to %sysfunc(countw(&ppx_list.));
		 			%let next_ppx = %scan(&ppx_list., &i.);
		 			ppx_&next_ppx. = (ICD_PRCDR_CD1 = "&next_ppx.");
		 		%end;
			 %mend ppx_loop;
		 
			 %ppx_loop;
		
		
		
			if (third_digit_prvdr='0') and
				(sixth_digit_prvdr not in ('E' 'F')) and
				(BENE_STATE_CD not in &nonus.) and
				(age_at_admsn >= 65.5) and
				(PTNT_DSCHRG_STUS_CD^='30');
		

		
			KEEP BENE_ID CLM_ID CLM_ADMSN_DT CLM_FROM_DT CLM_THRU_DT BENE_STATE_CD DOB_DT GNDR_CD NCH_BENE_DSCHRG_DT CLM_SRC_IP_ADMSN_CD
				ortho_adm_trad gensurg_adm_trad ortho_adm_mod gensurg_adm_mod
				era_trad era_mod admit_year data_year
				OP_PHYSN_UPIN OP_PHYSN_NPI AT_PHYSN_NPI AT_PHYSN_UPIN PRVDR_NUM OT_PHYSN_UPIN OT_PHYSN_NPI
				PRNCPAL_DGNS_CD ICD_DGNS_CD1-ICD_DGNS_CD25 CLM_POA_IND_SW1-CLM_POA_IND_SW25
				ICD_PRCDR_CD1-ICD_PRCDR_CD25 PRCDR_DT1-PRCDR_DT25
				NCH_CLM_TYPE_CD CLM_IP_ADMSN_TYPE_CD PTNT_DSCHRG_STUS_CD CLM_DRG_CD CLM_FAC_TYPE_CD PRVDR_STATE_CD ORG_NPI_NUM
				admit_year disch_year birth_dt ppx_dt age_at_admsn los 
				emergent_type urgent_type elective_type other_adm_type adm_type_missing
				ppx: age_65_69 age_70_74 age_75_79 age_80_85 age_85_plus CLM_DRG_CD
				age_at_admsn;
		run;
		
	
		**** Merge the mbsf and the inpatient files. ****;
	
		proc sort data=inpatient_base_claims_j_&cyear.;
			by BENE_ID;
		run;
	
		proc sort data=mbsf_ab_summary_&cyear.;
			by BENE_ID;
		run;
	
		data d_&cyear.;
			merge inpatient_base_claims_j_&cyear. (in=a) mbsf_ab_summary_&cyear. (in=b);
			by BENE_ID;
			miss_mbsf_flag=0;
			if a=1 and b=0 then miss_mbsf_flag=1;
			if a;
		run;
	
		proc datasets lib=work nolist nodetails;
			delete mbsf_ab_summary_&cyear. inpatient_base_claims_j_&cyear.;
		run;
	
		data d_&cyear.;
			set d_&cyear.;
	
			sex_male=0;
			sex_female=0;
			if BENE_SEX_IDENT_CD = '1' then sex_male = 1;
			else if BENE_SEX_IDENT_CD = '2' then sex_female = 1;
			else if BENE_SEX_IDENT_CD = ' ' then do;
  				if GNDR_CD='1' then sex_male=1;
  				else if GNDR_CD='2' then sex_female=1;
  			end;
  	
  			race_unknown = 0;
			race_nh_white = 0;
			race_black = 0;
			race_other = 0;
			race_asian_pi = 0;
			race_hispanic = 0;
			race_am_indian_ak_native = 0;

			if RTI_RACE_CD = '0' then race_unknown = 1;
			else if RTI_RACE_CD = '1' then race_nh_white = 1;
			else if RTI_RACE_CD = '2' then race_black = 1;
			else if RTI_RACE_CD = '3' then race_other = 1;
			else if RTI_RACE_CD = '4' then race_asian_pi = 1;
			else if RTI_RACE_CD = '5' then race_hispanic = 1;
			else if RTI_RACE_CD = '6' then race_am_indian_ak_native = 1;
			
			FLAG_any=0;
			
			IF (DOB_DT=.) THEN FLAG_dob_ip=1;
			IF (GNDR_CD=' ') THEN FLAG_gndr_ip=1;
			IF (BENE_BIRTH_DT=.) THEN FLAG_dob_mbsf=1;
			IF (BENE_SEX_IDENT_CD=' ') THEN FLAG_gndr_mbsf=1;
			IF (DOB_DT=. AND GNDR_CD=' ' AND BENE_BIRTH_DT=. AND BENE_SEX_IDENT_CD=' ')
				THEN FLAG_all_gndr_dob=1;
			IF ((FLAG_dob_ip=1 AND FLAG_dob_mbsf=1) OR
				(FLAG_gndr_ip=1 AND FLAG_gndr_mbsf=1) OR FLAG_all_gndr_dob=1 OR
				miss_mbsf_flag=1) THEN
			FLAG_any=1;

			IF FLAG_any = 0;
	
		run;
	
	
	%end;


%mend read_in_mbsf;

%read_in_mbsf;

data all_ip_mbsf;
	set d_1999
		d_2000
		d_2001
		d_2002
		d_2003
		d_2009
		d_2010
		d_2011
		d_2012
		d_2013;
run;

proc datasets lib=work nolist nodetails;
	delete d_1999 d_2000 d_2001 d_2002 d_2003 d_2009 d_2010 d_2011 d_2012 d_2013;
run;

data all_ip_mbsf;
set all_ip_mbsf;
	index_adm = ortho_adm_trad + gensurg_adm_trad+ortho_adm_mod+gensurg_adm_mod;
	if index_adm > 0;
run;

proc contents data=all_ip_mbsf;
run;


*******************************************;
**** Make HMO flags. **********************;


DATA dn_all;
LENGTH BENE_ID $15.
	hmo1-hmo180 $3.
	ptb1-ptb180 $3.
	era_two $3.;
RUN;


%macro byyear(yr1,yr2);

* Iterate once per year from 1999 to 2013;
%do yr=&yr1 %to &yr2;

* Each year has its own directory, so reassign libname with each iteration;
libname dn "/Young_Surgeon/sas/Medicare/&yr.";

* Assign a sequential number to each month, so Jan 1999=1, Feb 1999=2, 
  Jan 2000=13, Jan 2001=25, etc.;
%let a=%eval(1+((&yr-1999)*12));
%let b=%eval(12+((&yr-1999)*12));

data dn_&yr (compress=yes);
 set dn.mbsf_ab_summary_&yr (keep=bene_id bene_hmo_ind_: 
  bene_mdcr_entlmt_buyin_ind_: );
 array hmoo $ bene_hmo_ind_: ;
 array hmon $ hmo&a-hmo&b;
 array ptbo $ bene_mdcr_entlmt_buyin_ind_: ;
 array ptbn $ ptb&a-ptb&b;
 do i=1 to 12;
  hmon(i)=hmoo(i);
  ptbn(i)=ptbo(i);
  end;
  era_two='t';
  IF (&yr IN (2009 2010 2011 2012 2013)) THEN era_two='m';
 keep bene_id hmo&a-hmo&b ptb&a-ptb&b era_two;
 run;

proc sort data=dn_&yr nodupkey; by bene_id; run;

%end;
%mend byyear;

* Call macro;
%byyear(1999,2003);
%byyear(2009,2013);


* Create one large dataset with one row per patient and all years together;
data dn_all;
 merge dn_1999-dn_2003 dn_2009-dn_2013;
 by bene_id;
 run;

proc datasets lib=work; delete dn_1999-dn_2003 dn_2009-dn_2013; quit; run;


* Merge in patient admission dates;

proc sort data=all_ip_mbsf; by BENE_ID; run;

PROC SORT DATA=dn_all; BY BENE_ID; RUN;

data pats_dn;
 merge all_ip_mbsf (in=ina) dn_all (in=inb);
 by bene_id;
 if ina;
 
 * Calculate the number of months since the end of 1998, e.g. Jan 1999=1;
 dx_time=(12*(year(CLM_ADMSN_DT)-1999))+month(CLM_ADMSN_DT);
 dschrg_time=(12*(year(NCH_BENE_DSCHRG_DT)-1999))+month(NCH_BENE_DSCHRG_DT);
 dschrg_time_om=(12*(year(NCH_BENE_DSCHRG_DT)-1999))+month(NCH_BENE_DSCHRG_DT)+1;
 
 * Set initial state of flag variables to zero;
 hmo_flag=0;
 ptb_flag=0;
 hmo_flag_dschrg=0;
 ptb_flag_dschrg=0;
 
 * The six-month lookback period will be i-6 through i, with i being the 
   dx_time variable calculated above;
 * The MAX function prevents i from dropping below 1;
 array hmo $ hmo1-hmo180;
 array ptb $ ptb1-ptb180;
 
 do i=max(1,dx_time-6) to dx_time;
  if hmo(i) in ("2" "B" "C")  then hmo_flag=1;
  if (ptb(i) in ("0" "1" "A") OR ptb(i)=" ") then ptb_flag=1;
  end;
  
  IF (NCH_BENE_DSCHRG_DT ne .) THEN
  	DO i = dschrg_time to min(dschrg_time_om,180);
 		if hmo(i) in ("2" "B" "C") then hmo_flag_dschrg=1;
 		if (ptb(i) in ("1" "A")) then ptb_flag_dschrg=1;
 END;
 
 IF hmo_flag=1 THEN DELETE;
 IF ptb_flag=1 THEN DELETE;
 IF hmo_flag_dschrg=1 THEN DELETE;
 IF ptb_flag_dschrg=1 THEN DELETE;
 
 
KEEP BENE_ID CLM_ID CLM_ADMSN_DT CLM_FROM_DT CLM_THRU_DT BENE_STATE_CD DOB_DT BENE_BIRTH_DT
	GNDR_CD BENE_SEX_IDENT_CD NCH_BENE_DSCHRG_DT CLM_SRC_IP_ADMSN_CD RTI_RACE_CD
	race_unknown race_nh_white race_black race_other race_asian_pi race_hispanic race_am_indian_ak_native
	ortho_adm_trad gensurg_adm_trad ortho_adm_mod gensurg_adm_mod
	era_trad era_mod admit_year data_year
	OP_PHYSN_UPIN OP_PHYSN_NPI AT_PHYSN_NPI AT_PHYSN_UPIN PRVDR_NUM OT_PHYSN_UPIN OT_PHYSN_NPI
	PRNCPAL_DGNS_CD ICD_DGNS_CD1-ICD_DGNS_CD25 CLM_POA_IND_SW1-CLM_POA_IND_SW25
	ICD_PRCDR_CD1-ICD_PRCDR_CD25 PRCDR_DT1-PRCDR_DT25
	NCH_CLM_TYPE_CD CLM_IP_ADMSN_TYPE_CD PTNT_DSCHRG_STUS_CD CLM_DRG_CD CLM_FAC_TYPE_CD PRVDR_STATE_CD ORG_NPI_NUM
	los sex_male sex_female 
	emergent_type urgent_type elective_type other_adm_type adm_type_missing
	age_65_69 age_70_74 age_75_79 age_80_85 age_85_plus age_at_admsn
	CLM_DRG_CD ppx:
	hmo_flag ptb_flag hmo_flag_dschrg ptb_flag_dschrg;
 
run;

/* Adding transfer in informations */

%macro mkindex(year);

data index_&year. (keep=BENE_ID CLM_ID CLM_ADMSN_DT prvdr_num);
  set pats_dn;
  where data_year = &year;
run;

%mend mkindex;

%mkindex(1999);
%mkindex(2000);
%mkindex(2001);
%mkindex(2002);
%mkindex(2003);
%mkindex(2009);
%mkindex(2010);
%mkindex(2011);
%mkindex(2012);
%mkindex(2013);


%macro add_tx(yr, prev, lb);

* CREATE DATASET TO DEFINE TRANSFER-IN VARIABLES;
%let keeplb= bene_id clm_admsn_dt nch_bene_dschrg_dt clm_id prvdr_num ptnt_dschrg_stus_cd clm_src_ip_admsn_cd;

data lookback_ip_&yr;
  set b_&yr..inpatient_base_claims_j_&yr. (keep = &keeplb)
  %IF &lb = 1 %THEN %DO;
    b_&prev..inpatient_base_claims_j_&prev. (keep = &keeplb)
  %END;
  ;
  
  transfer_24hrs_ip = 1;
  rename 	clm_admsn_dt = lookback_admit
  		nch_bene_dschrg_dt = lookback_disch
  		prvdr_num = lookback_prvdr_num
  		ptnt_dschrg_stus_cd = lookback_status
  		clm_src_ip_admsn_cd = lookback_source
  		;
  run;
  
  
  * LOOK FOR EVIDENCE OF A PRIOR MEDICARE ADMISSION GOING BACK 24 HOURS;
  
  proc sql;
    CREATE TABLE match_lb_trans_&yr. AS
    SELECT 	a.bene_id, a.clm_id, a.clm_admsn_dt,
    		b.transfer_24hrs_ip, b.lookback_prvdr_num, b.lookback_status, b.lookback_source
    FROM 	index_&yr. AS a,
    		lookback_ip_&yr. AS b
    WHERE	a.bene_id = b.bene_id
    		AND a.clm_admsn_dt - 1 <= b.lookback_disch <= a.clm_admsn_dt
    		AND a.clm_id NE b.clm_id
    		AND a.prvdr_num NE b.lookback_prvdr_num
    ORDER BY	a.bene_id, a.clm_id, a.clm_admsn_dt
    ;
    QUIT;
  run;
  
  proc means data=match_lb_trans_&yr. noprint;
    by bene_id clm_id clm_admsn_dt;
    var transfer_24hrs_ip;
    output out=max_lb_trans_&yr (drop=_type_ _freq_) max=
    ;
  run;
  
  
  
  data lookback_ip_6moadm_&yr;
  set b_&yr..inpatient_base_claims_j_&yr. (keep = &keeplb)
  %IF &lb = 1 %THEN %DO;
    b_&prev..inpatient_base_claims_j_&prev. (keep = &keeplb)
  %END;
  ;
  
  past_adm_6mo = 1;
  rename 	clm_admsn_dt = lookback_admit
  		nch_bene_dschrg_dt = lookback_disch
  		prvdr_num = lookback_prvdr_num
  		ptnt_dschrg_stus_cd = lookback_status
  		clm_src_ip_admsn_cd = lookback_source
  		;
  run;
  
  
  
    * LOOK FOR EVIDENCE OF A PRIOR MEDICARE ADMISSION IN PAST 6 MONTHS;
  
  proc sql;
    CREATE TABLE match_lb_pastadm6mo_&yr. AS
    SELECT 	a.bene_id, a.clm_id, a.clm_admsn_dt,
    		b.past_adm_6mo
    FROM 	index_&yr. AS a,
    		lookback_ip_6moadm_&yr. AS b
    WHERE	a.bene_id = b.bene_id
    		AND a.clm_admsn_dt - 180 <= b.lookback_disch < a.clm_admsn_dt
    		AND a.clm_id NE b.clm_id
    ORDER BY	a.bene_id, a.clm_id, a.clm_admsn_dt
    ;
    QUIT;
  run;
  
  proc means data=match_lb_pastadm6mo_&yr. noprint;
    by bene_id clm_id clm_admsn_dt;
    var past_adm_6mo;
    output out=max_lb_pastadm6mo_&yr (drop=_type_ _freq_) max=
    ;
  run;
  
  
  
  
  
  
%mend add_tx;

  
%add_tx(1999, 1998, 0);
%add_tx(2000, 1999, 1);
%add_tx(2001, 2000, 1);
%add_tx(2002, 2001, 1);
%add_tx(2003, 2002, 1);
%add_tx(2009, 2008, 0);
%add_tx(2010, 2009, 1);
%add_tx(2011, 2010, 1);
%add_tx(2012, 2011, 1);
%add_tx(2013, 2012, 1);


proc sort data=pats_dn;
  by BENE_ID CLM_ID CLM_ADMSN_DT;
run;

data pats_dn;
  merge pats_dn (in=a)
  max_lb_trans_1999
  max_lb_trans_2000
  max_lb_trans_2001
  max_lb_trans_2002
  max_lb_trans_2003
  max_lb_trans_2009
  max_lb_trans_2010
  max_lb_trans_2011
  max_lb_trans_2012
  max_lb_trans_2013
  ;
  by BENE_ID CLM_ID CLM_ADMSN_DT;
  if a;
run;


data pats_dn;
  set pats_dn;
  if transfer_24hrs_ip=. then transfer_24hrs_ip = 0;
run;



data pats_dn;
  merge pats_dn (in=a)
  max_lb_pastadm6mo_1999
  max_lb_pastadm6mo_2000
  max_lb_pastadm6mo_2001
  max_lb_pastadm6mo_2002
  max_lb_pastadm6mo_2003
  max_lb_pastadm6mo_2009
  max_lb_pastadm6mo_2010
  max_lb_pastadm6mo_2011
  max_lb_pastadm6mo_2012
  max_lb_pastadm6mo_2013
  ;
  by BENE_ID CLM_ID CLM_ADMSN_DT;
  if a;
run;


data pats_dn;
  set pats_dn;
  if past_adm_6mo=. then past_adm_6mo = 0;
run;






********************************************************************;
**** Merge in comorbidities that were created by Alex Hill. ********;
**** Exclude some variables that we do not need for this study. ****;

data comorbs;
set h.silber_all (drop=silb_ALCOHOL silb_DRUG silb_SmokHx silb_WGHTLOSS silb_comorb_any);
run;

proc sort data=pats_dn;
by BENE_ID CLM_ID;
run;

proc sort data=comorbs;
by BENE_ID CLM_ID;
run;

data pre_linkage_data;
merge pats_dn (in=a) comorbs (in=b);
by BENE_ID CLM_ID;
if a=1 and b=1;
run;

**********************************************************************;
**** Create the spx variables. ***************************************;
**** This code is modified from Alex Hill's "spx.sas" program. ****;


data pre_linkage_data(drop=i);
set pre_linkage_data;

spx_minor_diagnostic=0;
spx_minor_therapeutic=0;
spx_major_diagnostic=0;
spx_major_therapeutic=0;

* Assign spx category;
array px $ icd_prcdr_cd1-icd_prcdr_cd25;
 do i=2 to 25;
  if px(i) in ("0021","0022","0023","0024","0025","0028","0029","0031","0032",
   "0033","0034","0035","0039","0058","0059","0067","0068","0069","0094","0110",
   "0111","0113","0116","0117","0331","0411","0611","0711","0819","1621",
   "1771","1811","1812","1819","2031","2121","2122","2129","2211","2219",
   "2411","2412","2419","2501","2509","2611","2619","2723","2724","2729",
   "2911","2912","2919","3141","3142","3143","3144","3148","3149","3321",
   "3322","3323","3324","3326","3423","3424","3425","3720","3721","3722",
   "3723","3725","3726","3727","3728","3729","3822","3823","3824","3825",
   "4131","4132","4138","4139","4222","4223","4224","4229","4412","4413",
   "4414","4419","4512","4513","4514","4516","4519","4522","4523","4524",
   "4525","4527","4528","4529","4822","4823","4824","4826","4829","4921",
   "4922","4923","4929","5011","5013","5110","5111","5112","5114","5115",
   "5211","5213","5214","5424","5425","5521","5522","5523","5631","5632",
   "5633","5635","5731","5732","5821","5822","5823","5824","5829","6011",
   "6013","6111","6119","6211","6301","6419","6811","6812","7021","7022",
   "751","7531","7532","7533","7534","7535","7538","8030","8031","8032",
   "8033","8034","8035","8036","8037","8038","8039","8511","8519","8611",
   "8619","8701","8702","8703","8704","8705","8706","8707","8708","8709",
   "8711","8712","8713","8714","8715","8716","8717","8721","8722","8723",
   "8724","8729","8731","8732","8733","8734","8735","8736","8737","8738",
   "8739","8741","8742","8743","8744","8749","8751","8752","8754","8759",
   "8761","8762","8763","8764","8765","8766","8769","8771","8772","8773",
   "8774","8775","8776","8777","8778","8779","8781","8782","8783","8784",
   "8785","8789","8791","8792","8793","8794","8795","8799","8801","8802",
   "8803","8804","8809","8811","8812","8813","8814","8815","8816","8819",
   "8821","8822","8823","8824","8825","8826","8827","8828","8829","8831",
   "8832","8833","8834","8835","8836","8837","8838","8839","8840","8841",
   "8842","8843","8844","8845","8846","8847","8848","8849","8850","8851",
   "8852","8853","8854","8855","8856","8857","8858","8859","8860","8861",
   "8862","8863","8864","8865","8866","8867","8868","8871","8872","8873",
   "8874","8875","8876","8877","8878","8879","8881","8882","8883","8884",
   "8885","8886","8889","8890","8891","8892","8893","8894","8895","8896",
   "8897","8898","8899","8901","8902","8903","8904","8905","8906","8907",
   "8908","8909","8910","8911","8912","8913","8914","8915","8916","8917",
   "8918","8919","8921","8922","8923","8924","8925","8926","8929","8931",
   "8932","8933","8934","8935","8936","8937","8938","8939","8941","8942",
   "8943","8944","8945","8946","8947","8948","8949","8950","8951","8952",
   "8953","8954","8955","8956","8957","8958","8959","8960","8961","8962",
   "8963","8964","8965","8966","8967","8968","8969","897","898","9001",
   "9002","9003","9004","9005","9006","9009","9011","9012","9013","9014",
   "9015","9016","9019","9021","9022","9023","9024","9025","9026","9029",
   "9031","9032","9033","9034","9035","9036","9039","9041","9042","9043",
   "9044","9045","9046","9049","9051","9052","9053","9054","9055","9056",
   "9059","9061","9062","9063","9064","9065","9066","9069","9071","9072",
   "9073","9074","9075","9076","9079","9081","9082","9083","9084","9085",
   "9086","9089","9091","9092","9093","9094","9095","9096","9099","9101",
   "9102","9103","9104","9105","9106","9109","9111","9112","9113","9114",
   "9115","9116","9119","9121","9122","9123","9124","9125","9126","9129",
   "9131","9132","9133","9134","9135","9136","9139","9141","9142","9143",
   "9144","9145","9146","9149","9151","9152","9153","9154","9155","9156",
   "9159","9161","9162","9163","9164","9165","9166","9169","9171","9172",
   "9173","9174","9175","9176","9179","9181","9182","9183","9184","9185",
   "9186","9189","9191","9192","9193","9194","9195","9196","9199","9201",
   "9202","9203","9204","9205","9209","9211","9212","9213","9214","9215",
   "9216","9217","9218","9219","9301","9302","9303","9304","9305","9306",
   "9307","9308","9309","9401","9402","9403","9408","9409","9411","9412",
   "9413","9419","9501","9502","9503","9505","9506","9507","9509","9511",
   "9512","9513","9514","9515","9516","9521","9522","9523","9524","9525",
   "9526","9541","9542","9543","9544","9545","9546","9547") 
   then spx_minor_diagnostic=1;
  else if px(i) in ("0001","0002",
   "0003","0009","0010","0011","0012","0013","0014","0015","0016","0017",
   "0018","0019","0040","0041","0042","0043","0044","0045","0046","0047",
   "0048","0049","0055","0060","0063","0064","0065","0074","0075","0076",
   "0077","0091","0092","0093","0095","0096","0101","0102","0109","0126",
   "0127","0241","0295","0296","038","0390","0391","0392","0395","0396",
   "042","0480","0481","0489","0531","0532","0539","0601","0801","0802",
   "0809","0881","0882","0883","0884","0885","0886","0887","0889","1423",
   "1424","1425","1433","1434","1435","1691","1741","1742","1743","1744",
   "1745","1749","1770","1781","1801","1802","1809","1829","184","2009",
   "201","208","2094","2100","2101","2102","2103","211","2130","2131",
   "2132","2171","2181","2191","2200","2201","2202","222","2301","2309",
   "2311","2319","232","233","2341","2342","2343","2349","235","236",
   "2370","2371","2372","2373","240","2431","2432","2439","246","247",
   "248","2491","2499","2551","2591","2592","2593","260","2691","2741",
   "2751","2752","2764","2791","2991","310","311","3193","3194","3195",
   "3201","3228","3331","3332","3333","3371","3372","3373","3378","3379",
   "3391","3401","3404","3405","3409","3471","3472","3491","3492","3541",
   "3604","3606","3607","370","3736","3770","3771","3772","3773","3778",
   "3781","3782","3783","3790","3792","3793","3891","3892","3893","3894",
   "3895","3897","3898","3899","3961","3962","3963","3964","3966","3990",
   "3995","3996","3997","411","4191","4192","4198","4233","4281","4292",
   "4299","4311","4319","4341","4422","4443","4444","4449","4462","4493",
   "4494","4530","4542","4543","4614","4624","4631","4632","4639","4685",
   "4686","4687","4695","4696","4831","4832","4833","4834","4836","4903",
   "4931","4941","4942","4943","4947","5091","5092","5093","5094","5099",
   "5101","5164","5184","5185","5186","5187","5188","5196","5197","5198",
   "5221","5284","5285","5286","5293","5294","5297","5298","5491","5496",
   "5497","5498","5499","5592","5593","5594","5595","5596","5691","570",
   "5711","5717","5792","5794","5795","583","5831","5839","586","5972",
   "598","5993","5994","5995","5996","5999","6071","6091","6092","610",
   "613","6141","6191","6291","6292","6352","636","6370","6371","6372",
   "6373","6384","6391","6491","6494","668","6691","670","6959","696",
   "697","6991","6992","6993","6994","6996","700","7011","7094","7095",
   "7121","720","721","7221","7229","7231","7239","724","7251","7252",
   "7253","7254","726","7271","7279","728","729","7301","7309","731",
   "7321","7322","733","734","7351","7359","736","738","7391","7392",
   "7393","750","752","7537","754","7562","7569","757","758","7591",
   "7592","7594","7671","7673","7675","7678","7693","7695","7696","7900",
   "7901","7902","7903","7904","7905","7906","7907","7908","7909","7970",
   "7971","7972","7973","7974","7975","7976","7977","7978","7979","8052",
   "8191","8192","8204","8292","8293","8294","8295","8296","8394","8395",
   "8396","8397","8398","8441","8442","8443","8445","8446","8447","8451",
   "8452","8453","8454","8455","8456","8457","8471","8472","8473","850",
   "8551","8552","8581","8591","8592","8601","8602","8603","8604","8605",
   "8607","8609","8623","8624","8626","8627","8628","863","8651","8659",
   "8664","8692","8699","9220","9221","9222","9223","9224","9225","9226",
   "9228","9229","923","9230","9231","9232","9233","9239","9241","9311",
   "9312","9313","9314","9315","9316","9317","9318","9319","9321","9322",
   "9323","9324","9325","9326","9327","9328","9329","9331","9332","9333",
   "9334","9335","9336","9337","9338","9339","9341","9342","9343","9344",
   "9345","9346","9351","9352","9353","9354","9355","9356","9357","9358",
   "9359","9361","9362","9363","9364","9365","9366","9367","9371","9372",
   "9373","9374","9375","9376","9377","9378","9381","9382","9383","9384",
   "9385","9389","9390","9391","9392","9393","9394","9395","9396","9397",
   "9398","9399","9421","9422","9423","9424","9425","9426","9427","9429",
   "9431","9432","9433","9434","9435","9436","9437","9438","9439","9441",
   "9442","9443","9444","9445","9446","9449","9451","9452","9453","9454",
   "9455","9459","9461","9462","9463","9464","9465","9466","9467","9468",
   "9469","9531","9532","9533","9534","9535","9536","9548","9549","9601",
   "9602","9603","9604","9605","9606","9607","9608","9609","9611","9614",
   "9615","9616","9617","9618","9619","9621","9622","9623","9624","9625",
   "9626","9627","9628","9629","9631","9632","9633","9634","9635","9636",
   "9637","9638","9639","9641","9642","9643","9644","9645","9646","9647",
   "9648","9649","9651","9652","9653","9654","9655","9656","9657","9658",
   "9659","966","9670","9671","9672","9701","9702","9703","9704","9705",
   "9711","9712","9713","9714","9715","9716","9721","9722","9723","9724",
   "9725","9726","9729","9731","9732","9733","9734","9735","9736","9737",
   "9738","9739","9741","9742","9743","9744","9749","9751","9752","9753",
   "9754","9755","9756","9759","9761","9762","9763","9764","9765","9769",
   "9771","9772","9773","9774","9775","9779","9781","9782","9783","9784",
   "9785","9786","9787","9788","9789","9801","9802","9803","9804","9805",
   "9811","9812","9813","9814","9815","9816","9817","9818","9819","9820",
   "9821","9822","9823","9824","9825","9826","9827","9828","9829","9851",
   "9852","9859","9900","9901","9902","9903","9904","9905","9906","9907",
   "9908","9909","9910","9911","9912","9913","9914","9915","9916","9917",
   "9918","9919","9920","9921","9922","9923","9924","9925","9926","9927",
   "9928","9929","9931","9932","9933","9934","9935","9936","9937","9938",
   "9939","9941","9942","9943","9944","9945","9946","9947","9948","9951",
   "9952","9953","9954","9955","9956","9957","9958","9959","9960","9961",
   "9962","9963","9964","9969","9971","9972","9973","9974","9975","9976",
   "9977","9978","9979","9981","9982","9983","9984","9985","9986","9988",
   "9991","9992","9993","9994","9995","9996","9997","9998","9999") 
   then spx_minor_therapeutic=1;
  else if px(i) in ("0112",
   "0114","0115","0118","0119","0332","0339","0412","0419","0511","0519",
   "0612","0613","0619","0712","0713","0714","0715","0716","0717","0719",
   "0811","0911","0912","0919","1021","1029","1121","1122","1129","1221",
   "1222","1229","1411","1419","1501","1509","1622","1623","1629","2032",
   "2039","2212","2502","2612","2721","2722","2811","2819","3145","3320",
   "3325","3327","3328","3329","3420","3421","3422","3426","3427","3428",
   "3429","3724","3821","3829","4011","4019","4133","4221","4225","4411",
   "4415","4511","4515","4521","4526","4821","4825","5012","5014","5019",
   "5113","5119","5212","5219","5421","5422","5423","5429","5524","5529",
   "5634","5639","5733","5734","5739","5921","5929","6012","6014","6015",
   "6018","6019","6212","6219","6309","6411","6511","6512","6513","6514",
   "6519","6611","6619","6711","6712","6719","6813","6814","6815","6816",
   "6819","6909","7023","7024","7029","7111","7119","7611","7619","7740",
   "7741","7742","7743","7744","7745","7746","7747","7748","7749","7880",
   "7881","7882","7883","7884","7885","7886","7887","7888","7889","8020",
   "8021","8022","8023","8024","8025","8026","8027","8028","8029","8198",
   "8321","8329","8512","8753","9504") then spx_major_diagnostic=1;
  else if px(i) in ("0050","0051","0052","0053","0054",
   "0056","0057","0061","0062","0066","0070","0071","0072","0073","0080",
   "0081","0082","0083","0084","0085","0086","0087","0120","0121","0122",
   "0123","0124","0125","0128","0129","0131","0132","0139","0141","0142",
   "0151","0152","0153","0159","016","0201","0202","0203","0204","0205",
   "0206","0207","0211","0212","0213","0214","022","0221","0222","0231",
   "0232","0233","0234","0235","0239","0242","0243","0291","0292","0293",
   "0294","0299","0301","0302","0309","031","0321","0329","034","0351",
   "0352","0353","0359","036","0371","0372","0379","0393","0394","0397",
   "0398","0399","0401","0402","0403","0404","0405","0406","0407","043",
   "0441","0442","0443","0444","0449","045","046","0471","0472","0473",
   "0474","0475","0476","0479","0491","0492","0493","0499","050","0521",
   "0522","0523","0524","0525","0529","0581","0589","059","0602","0609",
   "062","0631","0639","064","0650","0651","0652","066","067","0681",
   "0689","0691","0692","0693","0694","0695","0698","0699","0700","0701",
   "0702","0721","0722","0729","073","0741","0742","0743","0744","0745",
   "0749","0751","0752","0753","0754","0759","0761","0762","0763","0764",
   "0765","0768","0769","0771","0772","0779","0780","0781","0782","0783",
   "0784","0791","0792","0793","0794","0795","0798","0799","0820","0821",
   "0822","0823","0824","0825","0831","0832","0833","0834","0835","0836",
   "0837","0838","0841","0842","0843","0844","0849","0851","0852","0859",
   "0861","0862","0863","0864","0869","0870","0871","0872","0873","0874",
   "0891","0892","0893","0899","090","0920","0921","0922","0923","093",
   "0941","0942","0943","0944","0949","0951","0952","0953","0959","096",
   "0971","0972","0973","0981","0982","0983","0991","0999","100","101",
   "1031","1032","1033","1041","1042","1043","1044","1049","105","106",
   "1091","1099","110","111","1131","1132","1139","1141","1142","1143",
   "1149","1151","1152","1153","1159","1160","1161","1162","1163","1164",
   "1169","1171","1172","1173","1174","1175","1176","1179","1191","1192",
   "1199","1200","1201","1202","1211","1212","1213","1214","1231","1232",
   "1233","1234","1235","1239","1240","1241","1242","1243","1244","1251",
   "1252","1253","1254","1255","1259","1261","1262","1263","1264","1265",
   "1266","1267","1269","1271","1272","1273","1274","1279","1281","1282",
   "1283","1284","1285","1286","1287","1288","1289","1291","1292","1293",
   "1297","1298","1299","1300","1301","1302","1311","1319","132","133",
   "1341","1342","1343","1351","1359","1361","1362","1363","1364","1365",
   "1366","1369","1370","1371","1372","138","139","1390","1391","1400",
   "1401","1402","1421","1422","1426","1427","1429","1431","1432","1439",
   "1441","1449","1451","1452","1453","1454","1455","1459","146","1471",
   "1472","1473","1474","1475","1479","1481","1482","1483","149","1511",
   "1512","1513","1519","1521","1522","1529","153","154","155","156",
   "157","159","1601","1602","1609","161","1631","1639","1641","1642",
   "1649","1651","1652","1659","1661","1662","1663","1664","1665","1666",
   "1669","1671","1672","1681","1682","1689","1692","1693","1698","1699",
   "1711","1712","1713","1721","1722","1723","1724","1731","1732","1733",
   "1734","1735","1736","1739","1751","1752","1753","1754","1755","1756",
   "1761","1762","1763","1769","1821","1831","1839","185","186","1871",
   "1872","1879","189","190","1911","1919","1921","1929","193","194",
   "1952","1953","1954","1955","196","199","2001","2021","2022","2023",
   "2041","2042","2049","2051","2059","2061","2062","2071","2072","2079",
   "2091","2092","2093","2095","2096","2097","2098","2099","2104","2105",
   "2106","2107","2109","214","215","2161","2162","2169","2172","2182",
   "2183","2184","2185","2186","2187","2188","2189","2199","2231","2239",
   "2241","2242","2250","2251","2252","2253","2260","2261","2262","2263",
   "2264","2271","2279","229","242","244","245","251","252","253",
   "254","2559","2594","2599","2621","2629","2630","2631","2632","2641",
   "2642","2649","2699","270","271","2731","2732","2742","2743","2749",
   "2753","2754","2755","2756","2757","2759","2761","2762","2763","2769",
   "2771","2772","2773","2779","2792","2799","280","282","283","284",
   "285","286","287","2891","2892","2899","290","292","293","2931",
   "2932","2933","2939","294","2951","2952","2953","2954","2959","2992",
   "2999","3001","3009","301","3021","3022","3029","303","304","3121",
   "3129","313","315","3161","3162","3163","3164","3169","3171","3172",
   "3173","3174","3175","3179","3191","3192","3198","3199","320","3209",
   "321","3220","3221","3222","3223","3224","3225","3226","3227","3229",
   "323","3230","3239","324","3241","3249","325","3250","3259","326",
   "329","330","331","3334","3339","3341","3342","3343","3348","3349",
   "335","3350","3351","3352","336","3392","3393","3398","3399","3402",
   "3403","3406","341","343","344","3451","3452","3459","346","3473",
   "3474","3479","3481","3482","3483","3484","3485","3489","3493","3499",
   "3500","3501","3502","3503","3504","3505","3506","3507","3508","3509",
   "3510","3511","3512","3513","3514","3520","3521","3522","3523","3524",
   "3525","3526","3527","3528","3531","3532","3533","3534","3535","3539",
   "3542","3550","3551","3552","3553","3554","3555","3560","3561","3562",
   "3563","3570","3571","3572","3573","3581","3582","3583","3584","3591",
   "3592","3593","3594","3595","3596","3597","3598","3599","3600","3601",
   "3602","3603","3605","3609","3610","3611","3612","3613","3614","3615",
   "3616","3617","3619","362","363","3631","3632","3633","3634","3639",
   "3691","3699","3710","3711","3712","3731","3732","3733","3734","3735",
   "3737","374","3741","3749","375","3751","3752","3753","3754","3755",
   "3760","3761","3762","3763","3764","3765","3766","3767","3768","3774",
   "3775","3776","3777","3779","3780","3785","3786","3787","3789","3791",
   "3794","3795","3796","3797","3798","3799","3800","3801","3802","3803",
   "3804","3805","3806","3807","3808","3809","3810","3811","3812","3813",
   "3814","3815","3816","3818","3826","3830","3831","3832","3833","3834",
   "3835","3836","3837","3838","3839","3840","3841","3842","3843","3844",
   "3845","3846","3847","3848","3849","3850","3851","3852","3853","3855",
   "3857","3859","3860","3861","3862","3863","3864","3865","3866","3867",
   "3868","3869","387","3880","3881","3882","3883","3884","3885","3886",
   "3887","3888","3889","390","391","3921","3922","3923","3924","3925",
   "3926","3927","3928","3929","3930","3931","3932","3941","3942","3943",
   "3949","3950","3951","3952","3953","3954","3955","3956","3957","3958",
   "3959","3965","397","3971","3972","3973","3974","3975","3976","3977",
   "3978","3979","398","3981","3982","3983","3984","3985","3986","3987",
   "3988","3989","3991","3992","3993","3994","3998","3999","400","4021",
   "4022","4023","4024","4029","403","4040","4041","4042","4050","4051",
   "4052","4053","4054","4059","4061","4062","4063","4064","4069","409",
   "410","4100","4101","4102","4103","4104","4105","4106","4107","4108",
   "4109","412","4141","4142","4143","415","4193","4194","4195","4199",
   "4201","4209","4210","4211","4212","4219","4231","4232","4239","4240",
   "4241","4242","4251","4252","4253","4254","4255","4256","4258","4259",
   "4261","4262","4263","4264","4265","4266","4268","4269","427","4282",
   "4283","4284","4285","4286","4287","4289","4291","430","431","432",
   "433","4342","4349","435","436","437","4381","4382","4389","4391",
   "4399","4400","4401","4402","4403","442","4421","4429","4431","4432",
   "4438","4439","4440","4441","4442","445","4461","4463","4464","4465",
   "4466","4467","4468","4469","4491","4492","4495","4496","4497","4498",
   "4499","4500","4501","4502","4503","4531","4532","4533","4534","4541",
   "4549","4550","4551","4552","4561","4562","4563","4571","4572","4573",
   "4574","4575","4576","4579","458","4581","4582","4583","4590","4591",
   "4592","4593","4594","4595","4601","4602","4603","4604","4610","4611",
   "4612","4613","4620","4621","4622","4623","4640","4641","4642","4643",
   "4650","4651","4652","4660","4661","4662","4663","4664","4671","4672",
   "4673","4674","4675","4676","4679","4680","4681","4682","4691","4692",
   "4693","4694","4697","4699","470","4701","4709","471","4711","4719",
   "472","4791","4792","4799","480","481","4835","4840","4841","4842",
   "4843","4849","485","4850","4851","4852","4859","4861","4862","4863",
   "4864","4865","4866","4869","4871","4872","4873","4874","4875","4876",
   "4879","4881","4882","4891","4892","4893","4899","4901","4902","4904",
   "4911","4912","493","4939","4944","4945","4946","4949","4951","4952",
   "4959","496","4971","4972","4973","4974","4975","4976","4979","4991",
   "4992","4993","4994","4995","4999","500","5021","5022","5023","5024",
   "5025","5026","5029","503","504","5051","5059","5061","5069","5102",
   "5103","5104","5121","5122","5123","5124","5131","5132","5133","5134",
   "5135","5136","5137","5139","5141","5142","5143","5149","5151","5159",
   "5161","5162","5163","5169","5171","5172","5179","5181","5182","5183",
   "5189","5191","5192","5193","5194","5195","5199","5201","5209","522",
   "5222","523","524","5251","5252","5253","5259","526","527","5280",
   "5281","5282","5283","5291","5292","5295","5296","5299","5300","5301",
   "5302","5303","5304","5305","5310","5311","5312","5313","5314","5315",
   "5316","5317","5321","5329","5331","5339","5341","5342","5343","5349",
   "5351","5359","5361","5362","5363","5369","537","5371","5372","5375",
   "5380","5381","5382","5383","5384","539","540","5411","5412","5419",
   "543","544","545","5451","5459","5461","5462","5463","5464","5471",
   "5472","5473","5474","5475","5492","5493","5494","5495","5501","5502",
   "5503","5504","5511","5512","5531","5532","5533","5534","5535","5539",
   "554","5551","5552","5553","5554","5561","5569","557","5581","5582",
   "5583","5584","5585","5586","5587","5589","5591","5597","5598","5599",
   "560","561","562","5640","5641","5642","5651","5652","5661","5662",
   "5671","5672","5673","5674","5675","5679","5681","5682","5683","5684",
   "5685","5686","5689","5692","5693","5694","5695","5699","5712","5718",
   "5719","5721","5722","5741","5749","5751","5759","576","5771","5779",
   "5781","5782","5783","5784","5785","5786","5787","5788","5789","5791",
   "5793","5796","5797","5798","5799","580","581","5841","5842","5843",
   "5844","5845","5846","5847","5849","585","5891","5892","5893","5899",
   "5900","5901","5902","5903","5909","5911","5912","5919","593","594",
   "595","596","5971","5979","5991","5992","600","602","6021","6029",
   "603","604","605","6061","6062","6069","6072","6073","6079","6081",
   "6082","6093","6094","6095","6096","6097","6099","612","6142","6149",
   "6192","6199","620","622","623","6241","6242","625","6261","6269",
   "627","6299","631","632","633","634","6351","6353","6359","6381",
   "6382","6383","6385","6389","6392","6393","6394","6395","6399","640",
   "642","643","6441","6442","6443","6444","6445","6449","645","6492",
   "6493","6495","6496","6497","6498","6499","650","6501","6509","6521",
   "6522","6523","6524","6525","6529","653","6531","6539","654","6541",
   "6549","6551","6552","6553","6554","6561","6562","6563","6564","6571",
   "6572","6573","6574","6575","6576","6579","658","6581","6589","6591",
   "6592","6593","6594","6595","6599","660","6601","6602","6621","6622",
   "6629","6631","6632","6639","664","6651","6652","6661","6662","6663",
   "6669","6671","6672","6673","6674","6679","6692","6693","6694","6695",
   "6696","6697","6699","672","6731","6732","6733","6739","674","675",
   "6751","6759","6761","6762","6769","680","6821","6822","6823","6824",
   "6825","6829","683","6831","6839","684","6841","6849","685","6851",
   "6859","686","6861","6869","687","6871","6879","688","689","6901",
   "6902","6911","6919","6921","6922","6923","6929","693","6941","6942",
   "6949","6951","6952","6995","6997","6998","6999","7012","7013","7014",
   "7031","7032","7033","704","7050","7051","7052","7053","7054","7055",
   "7061","7062","7063","7064","7071","7072","7073","7074","7075","7076",
   "7077","7078","7079","708","7091","7092","7093","7101","7109","7122",
   "7123","7124","7129","713","714","715","7161","7162","7171","7172",
   "7179","718","719","7394","7399","740","741","742","743","744",
   "7491","7499","7536","7550","7551","7552","7561","7593","7599","7601",
   "7609","762","7631","7639","7641","7642","7643","7644","7645","7646",
   "765","7661","7662","7663","7664","7665","7666","7667","7668","7669",
   "7670","7672","7674","7676","7677","7679","7691","7692","7694","7697",
   "7699","7700","7701","7702","7703","7704","7705","7706","7707","7708",
   "7709","7710","7711","7712","7713","7714","7715","7716","7717","7718",
   "7719","7720","7721","7722","7723","7724","7725","7726","7727","7728",
   "7729","7730","7731","7732","7733","7734","7735","7736","7737","7738",
   "7739","7751","7752","7753","7754","7756","7757","7758","7759","7760",
   "7761","7762","7763","7764","7765","7766","7767","7768","7769","7770",
   "7771","7772","7773","7774","7775","7776","7777","7778","7779","7780",
   "7781","7782","7783","7784","7785","7786","7787","7788","7789","7790",
   "7791","7792","7793","7794","7795","7796","7797","7798","7799","7800",
   "7801","7802","7803","7804","7805","7806","7807","7808","7809","7810",
   "7811","7812","7813","7814","7815","7816","7817","7818","7819","7820",
   "7822","7823","7824","7825","7827","7828","7829","7830","7831","7832",
   "7833","7834","7835","7837","7838","7839","7840","7841","7842","7843",
   "7844","7845","7846","7847","7848","7849","7850","7851","7852","7853",
   "7854","7855","7856","7857","7858","7859","7860","7861","7862","7863",
   "7864","7865","7866","7867","7868","7869","7870","7871","7872","7873",
   "7874","7875","7876","7877","7878","7879","7890","7891","7892","7893",
   "7894","7895","7896","7897","7898","7899","7910","7911","7912","7913",
   "7914","7915","7916","7917","7918","7919","7920","7921","7922","7923",
   "7924","7925","7926","7927","7928","7929","7930","7931","7932","7933",
   "7934","7935","7936","7937","7938","7939","7940","7941","7942","7945",
   "7946","7949","7950","7951","7952","7955","7956","7959","7960","7961",
   "7962","7963","7964","7965","7966","7967","7968","7969","7980","7981",
   "7982","7983","7984","7985","7986","7987","7988","7989","7990","7991",
   "7992","7993","7994","7995","7996","7997","7998","7999","8000","8001",
   "8002","8003","8004","8005","8006","8007","8008","8009","8010","8011",
   "8012","8013","8014","8015","8016","8017","8018","8019","8040","8041",
   "8042","8043","8044","8045","8046","8047","8048","8049","805","8050",
   "8051","8053","8054","8059","806","8070","8071","8072","8073","8074",
   "8075","8076","8077","8078","8079","8080","8081","8082","8083","8084",
   "8085","8086","8087","8088","8089","8090","8091","8092","8093","8094",
   "8095","8096","8097","8098","8099","8100","8101","8102","8103","8104",
   "8105","8106","8107","8108","8109","8111","8112","8113","8114","8115",
   "8116","8117","8118","8120","8121","8122","8123","8124","8125","8126",
   "8127","8128","8129","8130","8131","8132","8133","8134","8135","8136",
   "8137","8138","8139","8140","8141","8142","8143","8144","8145","8146",
   "8147","8148","8149","8151","8152","8153","8154","8155","8156","8157",
   "8159","8161","8162","8163","8164","8165","8166","8169","8171","8172",
   "8173","8174","8175","8179","8180","8181","8182","8183","8184","8185",
   "8186","8187","8188","8193","8194","8195","8196","8197","8199","8201",
   "8202","8203","8209","8211","8212","8219","8221","8222","8229","8231",
   "8232","8233","8234","8235","8236","8239","8241","8242","8243","8244",
   "8245","8246","8251","8252","8253","8254","8255","8256","8257","8258",
   "8259","8261","8269","8271","8272","8279","8281","8282","8283","8284",
   "8285","8286","8289","8291","8299","8301","8302","8303","8309","8311",
   "8312","8313","8314","8319","8331","8332","8339","8341","8342","8343",
   "8344","8345","8349","835","8361","8362","8363","8364","8365","8371",
   "8372","8373","8374","8375","8376","8377","8379","8381","8382","8383",
   "8384","8385","8386","8387","8388","8389","8391","8392","8393","8399",
   "8400","8401","8402","8403","8404","8405","8406","8407","8408","8409",
   "8410","8411","8412","8413","8414","8415","8416","8417","8418","8419",
   "8421","8422","8423","8424","8425","8426","8427","8428","8429","843",
   "8440","8444","8448","8458","8459","8460","8461","8462","8463","8464",
   "8465","8466","8467","8468","8469","8480","8481","8482","8483","8484",
   "8485","8491","8492","8493","8494","8499","8520","8521","8522","8523",
   "8524","8525","8531","8532","8533","8534","8535","8536","8541","8542",
   "8543","8544","8545","8546","8547","8548","8550","8553","8554","8555",
   "856","857","8570","8571","8572","8573","8574","8575","8576","8579",
   "8582","8583","8584","8585","8586","8587","8589","8593","8594","8595",
   "8596","8599","8606","8621","8622","8625","864","8660","8661","8662",
   "8663","8665","8666","8667","8669","8670","8671","8672","8673","8674",
   "8675","8681","8682","8683","8684","8685","8686","8687","8689","8690",
   "8691","8693","8694","8695","8696","8697","8698","9227") 
   then spx_major_therapeutic=1;
  end;
  run;
 
 * Apply hierarchy;
 data pre_linkage_data;
 set pre_linkage_data;
 if spx_major_therapeutic=1 then do;
  spx_major_diagnostic=0; spx_minor_therapeutic=0; spx_minor_diagnostic=0;
  end; else if spx_major_diagnostic=1 then do;
  spx_minor_therapeutic=0; spx_minor_diagnostic=0;
  end; else if spx_minor_therapeutic=1 then do;
  spx_minor_diagnostic=0;
  end;
 spx_none=(spx_major_therapeutic=0 and spx_major_diagnostic=0 and 
  spx_minor_therapeutic=0 and spx_minor_diagnostic=0);
 run;



***************************;
**** Save the dataset. ****;

data f.pre_linkage_data_new_spx;
set pre_linkage_data;
run;

proc contents data=pre_linkage_data;
run;

